//
//  UIViewControllerExtension.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/29.
//

import Foundation

extension UIViewController {
    static func getCurrentVc() -> UIViewController? {
        var win: UIWindow?
        if #available(iOS 13, *) {
            let windowScene = UIApplication.shared.connectedScenes.first
            guard let windowScene = windowScene as? UIWindowScene, windowScene.activationState == .foregroundActive else {
                return nil
            }
            win = windowScene.windows.first
        } else {
            win = UIApplication.shared.keyWindow
        }
       
        guard let win = win else {
            return nil
        }
        
        var window = win
        if window.windowLevel == .normal {
            var windows: [UIWindow] = []
            
            if #available(iOS 15, *) {
                windows = UIApplication.shared.connectedScenes.map{$0 as? UIWindowScene}.compactMap{$0}.first?.windows ?? []
            } else {
                windows = UIApplication.shared.windows
            }
            
            for tmpWin in windows {
                if tmpWin.windowLevel == .normal {
                    window = tmpWin
                    break
                }
            }
        }
        
        var rootVc = window.rootViewController
        var activityVc: UIViewController?
        
        while true {
            if let vc = rootVc {
                if let vc = vc as? UINavigationController {
                    activityVc = vc.visibleViewController
                } else if let vc = vc as? UITabBarController {
                    activityVc = vc.selectedViewController
                } else if let vc = vc.presentedViewController {
                    activityVc = vc
                } else {
                    break
                }
                
                rootVc = activityVc
            } else {
                break
            }
        }
        
        return activityVc
    }
    
    @objc
    func showTip() {
        let vc = COTipViewController()
        vc.showVc = self
        self.present(vc, animated: true)
    }
    
    func follo(_ id: Int, isFollo: Bool) {
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: heProvider, target: isFollo ? .follo(id: id) : .cancelFollo(id: id)) { _ in
            self.view.hideToastActivity()
            NotificationCenter.default.post(name: NSNotification.Name("FolloSuccess"), object: nil)
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center)
        }
    }
    
    var currentController:UIViewController? {
        let delegate  = UIApplication.shared.delegate as? AppDelegate
        var current = delegate?.window?.rootViewController

        while (current?.presentedViewController != nil)  {
            current = current?.presentedViewController
        }
        if let tabbar = current as? UITabBarController , tabbar.selectedViewController != nil {
            current = tabbar.selectedViewController
        }
        while let navi = current as? UINavigationController , navi.topViewController != nil  {
            current = navi.topViewController
        }
        return current
    }
    
    // 跳转下一页把当前控制器移除 先无动画pop再push，替换当前控制器
    func pushWithNoPopController(vc:UIViewController){
        // 获取当前路由的控制器数组
        let vcArray = NSMutableArray(array: self.navigationController!.viewControllers)
        // 获取档期控制器在路由的位置
        let index = vcArray.index(of: self)
        // 移除当前路由器
        vcArray.removeObject(at: index)
        // 添加新控制器
        vcArray.add(vc)
        // 重新设置当前导航控制器的路由数组
        self.navigationController?.setViewControllers(vcArray as! [UIViewController], animated: true)
    }
    
    func showAlert(style: UIAlertController.Style, title: String?, message: String?, actions: [String], handler: @escaping (Int) -> Void) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: style)
        for i in 0..<actions.count {
            let action = UIAlertAction(title: actions[i], style: .default) { (action) in
                handler(i)
            }
            alert.addAction(action)
        }
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil);
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }
    
}
